 ;(function(w){
     w.damu={};//将damu绑给window,使用命名空间来解决命名冲突
     w.damu.css=function (node,type,val){
    if(typeof node==="object"&&typeof node["transform"]==="undefined"){
        node["transform"]={}
    }
    if(arguments.length>=3){
        var text="";
        node["transform"] [type]=val;
        for(item in  node["transform"]){
            if( node["transform"].hasOwnProperty(item)){//如果item是对象的直接属性
            switch (item) {
                case "translateX":
                case "translateY":
                    text +=item+"("+node["transform"] [item]+"px)"
                    break;
                    case "scale":
                    text +=item+"("+node["transform"] [item]+")"
                    break;
                    case "rotate":
                    text +=item+"("+node["transform"] [item]+"deg)"
                    break;
            }
            }
        }
            node.style.transform=node.style.webkitTransform=text;
       
    }else if(arguments.length===2){
        val=node["transform"][type];
        if(typeof val==="undefined"){
        switch (type) {
                case "translateX":
                case "translateY":
                case "rotate":
                 val=0;
                break;
                case "scale":
                 val=1;
                break;
        }
        }
        return val;
    }
     }
     w.damu.carousel=function (arr){
        var carouselWrap=document.querySelector(".carousel-wrap"); 
        var pointslength=arr.length;
        if(carouselWrap){ 
            //无缝
            var needCarousel=carouselWrap.getAttribute("needcarousel")
            needCarousel=needCarousel==null?false:true;
            if(needCarousel){
               
                arr=arr.concat(arr);
            }  
            var ulNode=document.createElement("ul");
            var styleNode=document.createElement("style")
            ulNode.className="list";
        }
            for(var i=0;i<arr.length;i++){   
            ulNode.innerHTML+='<li><a href="javascript:;"><img src="'+arr[i]+'"/></a></li>';  
            }
            styleNode.innerHTML=".carousel-wrap>.list>li{width: "+(1/arr.length*100)+"%;}.carousel-wrap>.list{width: "+arr.length+"00%;}";
            carouselWrap.appendChild(ulNode);
            document.head.appendChild(styleNode);
            setTimeout(function(){
                var imgNodes=document.querySelector(".list > li > a > img");
                carouselWrap.style.height=imgNodes.offsetHeight+"px";
            },100) 
        var pointWrap=document.querySelector(".carousel-wrap>.point-wrap"); 
        var needPoint=carouselWrap.getAttribute("needpoint")
            needPoint=needPoint==null?false:true;
            if(needPoint){
           
        if(pointWrap){ 
            for(var i=0;i<pointslength;i++){  
                if(i==0){
                    pointWrap.innerHTML+="<span class='active'></span>";
                    }
                    else{
                        pointWrap.innerHTML+="<span></span>"; 
                    } 
            }
            var pointsSpan=document.querySelectorAll(".carousel-wrap>.point-wrap>span");
            }
        }
        //滑屏
        // 手指一开始的位置
        // 元素一开始的位置
        // 手指move的实时距离
        // 把手指移动的距离加给元素
        var startX=0;
        var elementX=0;
        var startY=0;
        var elementY=0;
        var disX=0;
        var index=0;
        var isX=true;
        var isFirst=true;
        //var translateX=0;
        carouselWrap.addEventListener("touchstart",function(ev){
            ev=ev||event;
            var touchC=ev.changedTouches[0];//第一根手指触摸的位置
            ulNode.style.transition="none";
    
            //无缝
            if(needCarousel){
                var index=damu.css(ulNode,"translateX")/document.documentElement.clientWidth;
        //index代表ul的位置,判断里边的index是索引值,两者是正负关系
            if(-index===0){
                index=-pointslength-1;
            }else if(-index==(arr.length-1)){
                index=-(pointslength-1);
            }
            damu.css(ulNode,"translateX",index*document.documentElement.clientWidth);//偏移
            }  
            startX=touchC.clientX;
            startY=touchC.clientY;
            elementX=damu.css(ulNode,"translateX");
            elementY=damu.css(ulNode,"translateY");
            clearInterval(timer);//清除定时器
             isX=true;
             isFirst=true;
        })
        carouselWrap.addEventListener("touchmove",function(ev){
            if(!isX){//二次以后的防抖动
                return;
            }
            ev=ev||event;
            var touchC=ev.changedTouches[0];
            nowX=touchC.clientX;
            nowY=touchC.clientY;
              disX=nowX-startX;//手指移动的距离
              disY=nowY-startY;
              if(isFirst){//首次判断用户的滑动方向
                isFirst=false;
                if(Math.abs(disY)>Math.abs(disX)){//说明在y轴上滑动
                    isX=false;
                       return;//首次防抖动
                   }
              }
           damu.css(ulNode,"translateX",elementX+disX);
        })
        carouselWrap.addEventListener("touchend",function(ev){
            ev=ev||event;
            index=damu.css(ulNode,"translateX")/document.documentElement.clientWidth;
            index=Math.round(index);//2分之一跳转
            // if(disX>0){//左右滑动无论多少都实现跳转
            //     index=Math.ceil(index)
            // }else if(disX<0){
            //     index=Math.floor(index)
            // }
            if(index>0){//左右滑动无论多少都实现跳转
                 index=0;
            }
            else if(index<1-arr.length){
                index=1-arr.length;
            }
            //无缝
          
            if(needPoint){
               
                point(index);
            } 
            
            //  translateX=index*(document.documentElement.clientWidth);
            // ulNode.style.transform='translateX('+translateX+'px)';
            damu.css(ulNode,"translateX",index*(document.documentElement.clientWidth));
            if(needAuto){
               
               auto();
           }  
      
        }) 
        //自动轮播
        var timer=0;
        var needAuto=carouselWrap.getAttribute("needauto")
        needAuto=needAuto==null?false:true;
            if(needAuto){ 
                auto();
            }  
        function auto(){
            clearInterval(timer);
            timer=setInterval(function(){
                if(index===1-arr.length){
                    ulNode.style.transition="none";
                    index=1-arr.length/2;
                    damu.css(ulNode,"translateX",index*(document.documentElement.clientWidth))
                }
                setTimeout(function(){
                    index--;
                    if(needPoint){     
               point(index);
           } 
                ulNode.style.transition="1s transform";
                damu.css(ulNode,"translateX",index*(document.documentElement.clientWidth))
                },50)
            },2000)
        }
        if(needPoint){
        function point(index){
            for(var i=0;i<pointsSpan.length;i++){
                pointsSpan[i].classList.remove("active");
            }
            pointsSpan[-index%pointslength].classList.add("active");
            ulNode.style.transition=".5s transform";
        }       
         }
        }

 })(window)
 